/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.adcc.webapp.action;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.activation.FileTypeMap;
import javax.servlet.http.HttpServletRequest;
import vn.adcc.model.Document;
import vn.adcc.service.DocumentManager;

/**
 *
 * @author ducanh.nguyen
 */
public class FileList extends BasePage implements Serializable {

    private DocumentManager documentManager;
    private Document document = new Document();

    public FileList() {
        setSortColumn("name");
    }
    private String query;
    private HttpServletRequest request = getRequest();
    private String folderPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/resources/"+FileUpload.UPLOAD_FOLDER_NAME+"/";
    public List<Document> getDocuments() {

        String uploadFolderContentType = getServletContext().getRealPath("\\resources") + "\\" + FileUpload.UPLOAD_FOLDER_NAME + "\\";
        File uploadFolder = new File(uploadFolderContentType);
        File[] files = uploadFolder.listFiles();

        if (files != null) {
            List<Document> docs = new ArrayList<Document>();
            Document doc;
            for (File file : files) {
                doc = new Document();
                doc.setName(file.getName());
                doc.setFilePath(folderPath+file.getName());
                doc.setFileSize(file.length());
                doc.setContentType(FileTypeMap.getDefaultFileTypeMap().getContentType(file));
                doc.setDateCreated(new Date(file.lastModified()));
                docs.add(doc);
            }
            return docs;
        }
        return null;
    }

    public String search() {
        return "document";
    }

    /**
     * @return the query
     */
    public String getQuery() {
        return query;
    }

    /**
     * @param query the query to set
     */
    public void setQuery(String query) {
        this.query = query;
    }

    /**
     * @return the documentManager
     */
    public DocumentManager getDocumentManager() {
        return documentManager;
    }

    /**
     * @param documentManager the documentManager to set
     */
    public void setDocumentManager(DocumentManager documentManager) {
        this.documentManager = documentManager;
    }

    /**
     * @return the document
     */
    public Document getDocument() {
        return document;
    }

    /**
     * @param document the document to set
     */
    public void setDocument(Document document) {
        this.document = document;
    }
}
